<?php
/* +----------------------------------------------------------------------
 * 作          者：huk
 +----------------------------------------------------------------------
 * 创建日期：2016-09-12
 +----------------------------------------------------------------------
 * 文件描述：指标题项管理
 +----------------------------------------------------------------------
 * 升级记录： 
 +----------------------------------------------------------------------
*/
class QuotaItemAction extends CommonAction {
	public function index() {
		$model = new QuotaItemModel();
		$where[] = "a.quotaid = b.id and a.formid = c.id";
		if(!empty($_REQUEST['search_keyword'])) {
			$where['b.code|c.title'] = array('like', '%'.$this->_request('search_keyword').'%');
		}
		//查询指标数据
		$data1 = $model->table("quota_item a,
							   quota b,
							   form c")
					  ->field("a.id id,
					  		   a.formid, 
					  		   a.objid, 
					  		   a.quotaid,
					  		   b.id quotaid,
					  		   b.code,
					  		   get_field_dict_name('quota','third_quota',b.third_quota) value,
					  		   c.id formid,
							   c.title")
					  ->where($where)
					  ->select();
       	foreach($data1 as $key => $value) {
       		$obj = explode(':', $value['objid']);
       	    if($obj[0] == 'affix') {
	        	$affixmodel = new AffixModel();
	        	$affixdata = $affixmodel->field("id,title,code")->where("id = '".$obj[1]."'")->find();
	        	$data1[$key]['typename'] = '附件题';
	        	$data1[$key]['objcode'] = $affixdata['code']; 
	        	$data1[$key]['objtitle'] = $affixdata['title'];
	        }
	        if($obj[0] == 'select') {
	        	$selectmodel = new QuestionTestSelectModel();
	        	$selectdata = $selectmodel->field("id,question,code")->where("id = '".$obj[1]."'")->find();
	        	$data1[$key]['typename'] = '选择题';
	        	$data1[$key]['objcode'] = $selectdata['code'];
	        	$data1[$key]['objtitle'] = $this->cutstr_html($selectdata['question']);
	        }
			if($obj[0] == 'blank') {
	        	$blankmodel = new BlankModel();
	        	$blankdata = $blankmodel->field("id,question,code")->where("id = '".$obj[1]."'")->find();
	        	$data1[$key]['typename'] = '填空题';
	        	$data1[$key]['objcode'] = $blankdata['code'];
				$data1[$key]['objtitle'] = $this->cutstr_html($blankdata['question']);
	        }
       	}
		$this->assign('data', $data1);//绑定题项数据
	  	$this->display();
	}
	  
	  
	/**
	 * 显示添加页面
	 */
	public function add() {
		//显示指标项
		$quotamodel = new QuotaModel();
		$quotadata = $quotamodel->field('id, 
										 code, 
										 value')
								->order("third_quota")
					  	        ->select();
		//显示问卷项
		$formid = $this->_request('formid');
		$mode = 'edit';
		$this->assign("formid", $formid);
		$form_model = new FormModel();
		$form = $form_model->field('id, 
									title')
					  	   ->select();
		//显示题项
		$formid = $this->_request('formid');
		$formdata = $form_model->field('id, 
										title, 
										intro, 
										item_info, 
										status')
					   		   ->where("id = '".$formid."'")
					   		   ->find();
		if(!empty($formdata['item_info'])) {
			$item_info = array();
			$item_info = explode(',', $formdata['item_info']);
			$item_list = array();
			foreach($item_info as $key => $value) {
				//根据模块类型获取模块内容
				$item_info_detail = explode(':', $value);
	       	    if($item_info_detail[0] == 'affix') {
		        	$affixmodel = new AffixModel();
		        	$affixdata = $affixmodel->field("id,title,code")->where("id = '".$item_info_detail[1]."'")->find();
		        	if(!empty($affixdata)) {
			        	$item_list[$key]['objtype'] = $item_info_detail[0];
			        	$item_list[$key]['objtypename'] = '附件题';
			        	$item_list[$key]['objid'] = $affixdata['id'];
			        	$item_list[$key]['objcode'] = $affixdata['code']; 
			        	$item_list[$key]['objtitle'] = $affixdata['title'];
		        	}
		        }
		        if($item_info_detail[0 ] == 'select') {
		        	$selectmodel = new QuestionTestSelectModel();
		        	$selectdata = $selectmodel->field("id,question,code")->where("id = '".$item_info_detail[1]."'")->find();
		        	if(!empty($selectdata)) {
			        	$item_list[$key]['objtype'] = $item_info_detail[0];
			        	$item_list[$key]['objtypename'] = '选择题';
			        	$item_list[$key]['objid'] = $selectdata['id'];
			        	$item_list[$key]['objcode'] = $selectdata['code'];
			        	$item_list[$key]['objtitle'] = $this->cutstr_html($selectdata['question']);
		        	}
		        }
				if($item_info_detail[0] == 'blank') {
		        	$blankmodel = new BlankModel();
		        	$blankdata = $blankmodel->field("id,question,code")->where("id = '".$item_info_detail[1]."'")->find();
		        	if(!empty($blankdata)) {
			        	$item_list[$key]['objtype'] = $item_info_detail[0];
			        	$item_list[$key]['objtypename'] = '填空题';
			        	$item_list[$key]['objid'] = $blankdata['id'];
			        	$item_list[$key]['objcode'] = $blankdata['code'];
						$item_list[$key]['objtitle'] = $this->cutstr_html($blankdata['question']);
		        	}
		        }
			}
		}
		$this->assign('quota_list', $quotadata);//绑定题项数据
		$this->assign('form_list', $form);//绑定问卷信息
		$this->assign('item_list', $item_list);//绑定题项信息
		$this->assign('type_list', set_field_dict_list('groups', 'type'));
		$this->display();
	}
	
	/**
	 * 将数据插入到数据库 
	 */
	public function insert() {
		$model = new QuotaItemModel();
		$adddata['formid'] = $this->_request('formid');
		$adddata['quotaid'] = $this->_request('quotaid');
		$adddata['objid'] = $this->_request('question');
		$adddata['type'] = strstr($this->_request('question'), ':', 1);
		$adddata['questionid'] = substr(strstr($this->_request('question'), ':'), 1);
		if(false !== $model->create($adddata, 1)) {
			if(false !== $model->add()) {
				$this->success('添加成功',__APP__.'?g=Admin&m=QuotaItem&a=index');
			} else {
				$this->error('添加失败');
			}
		} else {
			$this->error($model->getError());
		}
	}
	
	/**
	 * 删除
	 */
	public function delete() {
		$deleteId = $this->_request('ids');
		if(!empty($deleteId) && is_array($deleteId)) {
			$model = new QuotaItemModel();
			$id = implode("','", $deleteId);	// 用','分割id数组，以便于拼装sql语句
			$id = "'".$id."'";	// 拼装sql语句，结果为'firstId','secondId',…
			if($data = $model->where("id in(".$id.")")->delete()) {
				redirect($_SERVER['HTTP_REFERER']);
			} else {
				$this->error('删除失败！');
			}
		} else {
			$this->error('您未选中需要删除的信息');
		}
	
	}
}
?>